<?php

namespace app\common\model;

use catcher\Utils;
use think\Collection;
use think\Paginator;
use app\common\model\search\FollowTrialSearch;

class FollowTrial extends BaseModel
{
    use FollowTrialSearch;

    protected $name = "follow_trial";
    
    public function getStatusTextAttr($value, $data): string
    {
        $examine = config('dictionary.trial_examine');
        if (!isset($data["status"])) {
            return '';
        }
        return $examine[$data["status"]]??'';
    }

    /**
     * 查询列表
     *
     * @time 2020年04月28日
     * @param array $where 条件
     * @param string $field 字段
     * @return Paginator
     */
    public function getList(array $where=[], string $field='*'): Paginator
    {
       # $users = $this->getUserIds();
       # if (!empty($users)) {
       #     $where[] = ['creator_id', 'in', $users];
       # }

        // 分页列表
        return $this->mongoSearch([],[],['patientId'])
            ->where($where)
            ->field($field)
            ->order('_id', 'desc')
            ->paginate($this->pageLimit)
            ->each(function ($item, $key){
                $item['doctor_mobile'] = $item['doctor_mobile']??'';
                $item['family_name'] = $item['family_name']??'';
                $item['duration'] = $item['duration']??'';
                return $item;
            });
    }

    /**
     * 添加数据
     *
     * @param array $param
     * @return string
     */
    public function addData(array $param): string
    {
        //添加时销毁自带的主键id
        unset($param['id']);
        return $this->save($param);
    }

    /**
     * 更新数据
     * @param array $param
     * @return bool
     */
    public function saveData($id, array $param): bool
    {
        $model = self::find($id);
        return $model->save($param);
    }

    /**
     * 获取数据
     * @param array $where 条件
     * @param string $field 字段
     * @return Collection
     */
    public function getDatas(array $where = [], string $field="*"): Collection
    {
        return $this->mongoSearch([],[],['patientId', 'title'])
                    ->field($field)
                    ->where($where)
                    ->order('_id', 'desc')
                    ->select()
                    ->hidden(['id']);
    }
}
